package Others;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

import MainMethods.FileWr;
import MainMethods.HoverImage;
import MainMethods.Screenshot;

public class Account {
	HoverImage hover = new HoverImage();
	Screenshot scsh = new Screenshot();
	Date date = new Date();
	SimpleDateFormat dateFormatter = new SimpleDateFormat("MM.dd.yyyy");

	public void accountWorks(WebDriver driver, FileWr file) {
		// Select "My Account" from the more stuff button or the upper-right
		// arrow
		try {
			Thread.sleep(3000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		// WebElement account;
		// driver.findElement(By.xpath("//*[@id=\"account_container\"]/div[1]"))
		// .click();
		// account = driver.findElement(By
		// .xpath("//*[@id=\"account_container\"]/div[2]/ul/li[1]/a"));
		// account.click();
		driver.findElement(
				By.xpath("//*[@id=\"aside-member-account-link\"]/span"))
				.click();
		try {
			Thread.sleep(3000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		if (hover.doesElementExist(driver, By.xpath("//*[@id=\"browse_wrapper\"]/div/div[2]")) == true){
			file.writeFile("Select 'My Account' from the more stuff button or the upper-right arrow - PASSED");
			System.out.println("1");
		}
//		if (driver.getCurrentUrl().equals(
//				"https://staging.membersarea.jomediainc.com/#member/account")) {
//			file.writeFile("Select 'My Account' from the more stuff button or the upper-right arrow - PASSED");
//			System.out.println("1");
	//	} 
	else {
			file.writeFile("Select 'My Account' from the more stuff button or the upper-right arrow - FAILED");
			System.out.println("-1");
		}
		try {
			Thread.sleep(3000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		// change password, and save changes
		driver.findElement(By.xpath("//*[@id=\"account-password\"]")).click();
		driver.findElement(By.xpath("//*[@id=\"old-pass\"]"))
				.sendKeys("123123");
		driver.findElement(By.xpath("//*[@id=\"new-pass\"]"))
				.sendKeys("321321");
		driver.findElement(By.xpath("//*[@id=\"conf-pass\"]")).sendKeys(
				"321321");
		try {
			Thread.sleep(2000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		driver.findElement(
				By.xpath("//*[@id=\"update-pass\"]/fieldset[4]/input")).click();
		if (hover.doesElementExist(driver, By.className("massege")) == true) {
			file.writeFile("Password change - PASSED");
			file.writeFile("new password: 321321");
			System.out.println("2");
		} else {
			file.writeFile("Password change - FAILED");
			System.out.println("-2");
		}
		// change e-mail and save changes
		// driver.navigate().refresh();
		try {
			Thread.sleep(3000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		driver.findElement(By.xpath("//*[@id=\"account-email\"]")).click();
		driver.findElement(By.xpath("//*[@id=\"username\"]")).sendKeys(
				dateFormatter.format(date) + "test_fox@jo-media.com");
		driver.findElement(By.xpath("//*[@id=\"conf-user\"]")).sendKeys(
				dateFormatter.format(date) + "test_fox@jo-media.com");
		try {
			Thread.sleep(2000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		driver.findElement(
				By.xpath("//*[@id=\"update-user\"]/fieldset[3]/input")).click();
		try {
			Thread.sleep(2000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		if (hover.doesElementExist(driver, By.className("message")) == true) {
			file.writeFile("E-MAIL change - PASSED");
			file.writeFile("new e-mail:" + dateFormatter.format(date)
					+ "test_fox@jo-media.com");
			System.out.println("2");
		} else {
			file.writeFile("E-MAIL change - FAILED");
			System.out.println("-2");
		}
	}

	// CANCEL MEMBERSHIP
	public void cancelMembership(WebDriver driver, FileWr file, String screenDir) {
		// Click on "Support" (upper-right or left menu)
		try {
			try {
				Thread.sleep(3000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			WebElement support;
			driver.findElement(
					By.xpath("//*[@id=\"account_container\"]/div[1]")).click();
			support = driver.findElement(By
					.xpath("//*[@id=\"account_container\"]/div[2]/ul/li[2]/a"));
			support.click();
			try {
				Thread.sleep(3000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			if (hover
					.doesElementExist(
							driver,
							By.xpath("//*[@id=\"browse_wrapper\"]/div/div/div[1]/div/div[1]/a")) == true) {
				file.writeFile("Select 'Support' from the upper-right arrow - PASSED");
				System.out.println("1");
			} else {
				file.writeFile("Select 'Support' from the more stuff button or the upper-right arrow - FAILED");
				scsh.makeScreen("support failed", screenDir);
				System.out.println("-1");
			}
			// Click on cancel membership
			driver.findElement(
					By.xpath("//*[@id=\"browse_wrapper\"]/div/div/div[1]/div/div[1]/a"))
					.click();
			try {
				Thread.sleep(3000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			driver.findElement(
					By.xpath("//*[@id=\"browse_wrapper\"]/section[2]/section[3]/a[2]"))
					.click();
			try {
				Thread.sleep(3000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			if (hover.doesElementExist(driver,
					By.xpath("//*[@id=\"browse_wrapper\"]/div/div[2]/div/div[2]")) == true) {
				file.writeFile("Click on cancel membership - PASSED");
				System.out.println("1");
				driver.findElement(By.id("first_name"))
						.sendKeys("qwerty");
				driver.findElement(By.id("last_name"))
						.sendKeys("qwerty");
				driver.findElement(By.id("phone")).sendKeys(
						"123456");
				driver.findElement(
						By.xpath("//*[@id=\"confirm_cancellation\"]")).click();
				driver.findElement(By.id("cancel_membership")).click();
				try {
					Thread.sleep(3000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				
				if (hover.doesElementExist(driver, By.className("checkmark")) == true) {
					file.writeFile("Fill all fields (Name, last name, and phone), check the message and submit - PASSED");
					System.out.println("1");
				} else {
					file.writeFile("Fill all fields (Name, last name, and phone), check the message and submit - FAILED");
					System.out.println("-1");
				}
			} else {
				file.writeFile("Click on cancel membership - FAILED");
				System.out.println("1");
			}
			driver.findElement(
					By.xpath("//*[@id=\"sidenav-more\"]/ul/li[3]/a/span"))
					.click();
			try {
				Thread.sleep(2000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			// make screenshot of FAQ
			scsh.makeScreen("After Cancel membership PASSED", screenDir);
			try {
				Thread.sleep(2000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		} catch (NoSuchElementException e) {
			file.writeFile("Cancel membership - FAILED");
			scsh.makeScreen("Cancel membership - FAILED", screenDir);
		}
	}
}
